package org.example.service.impl;

import cn.hutool.json.JSONUtil;
import com.fazecast.jSerialComm.SerialPort;
import gnu.io.CommPortIdentifier;
import org.example.dto.OrderDTO;
import org.example.service.OrderService;
import org.springframework.stereotype.Service;

import javax.print.*;
import javax.print.attribute.AttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.*;
import javax.print.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Override
    public OrderDTO getOrderById(String orderId) {
        // 这里应该是从数据库获取订单，这里使用模拟数据
        return createMockOrder(orderId);
    }

    private OrderDTO createMockOrder(String orderId) {
        OrderDTO order = new OrderDTO();
        order.setOrderId(orderId);
        order.setOrderTime(LocalDateTime.now());
        order.setCustomerName("张三");
        order.setCustomerPhone("13800138000");
        order.setCustomerAddress("北京市海淀区中关村大街1号");

        // 添加商品
        OrderDTO.OrderItemDTO item1 = new OrderDTO.OrderItemDTO();
        item1.setProductName("无线鼠标");
        item1.setQuantity(2);
        item1.setUnitPrice(new BigDecimal("99.95"));
        item1.setSubtotal(item1.getUnitPrice().multiply(new BigDecimal(item1.getQuantity())));

        OrderDTO.OrderItemDTO item2 = new OrderDTO.OrderItemDTO();
        item2.setProductName("机械键盘");
        item2.setQuantity(1);
        item2.setUnitPrice(new BigDecimal("459.00"));
        item2.setSubtotal(item2.getUnitPrice().multiply(new BigDecimal(item2.getQuantity())));

        List<OrderDTO.OrderItemDTO> items = new ArrayList<>();
        items.add(item1);
        items.add(item2);
        order.setItems(items);

        // 计算金额
        BigDecimal total = item1.getSubtotal().add(item2.getSubtotal());
        order.setTotalAmount(total);
        order.setDiscount(new BigDecimal("50.00"));
        order.setPaymentAmount(total.subtract(order.getDiscount()));

        return order;
    }




}
